package 代码题.多线程并发工具类的题目;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * 测试threadLocal的多线程隔离性
 * 测试可继承threadlocal的子线程共享父线程数据，但依旧与并行线程隔离
 */
public class TestThreadLocal2 {

    public static final InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
    public static final ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        //main线程往inheritableThreadLocal设置值
        inheritableThreadLocal.set("Inheritable hello");
        //main线程往threadLocal设置值
        threadLocal.set("hello");

        new Thread(() -> {
            System.out.println(String.format("inheritableThreadLocal子线程值：%s", inheritableThreadLocal.get()));
            System.out.println(String.format("threadlocal中子线程值：%s", threadLocal.get()));

            new Thread(() -> {
                System.out.println(String.format("inheritableThreadLocal孙子线程值：%s", inheritableThreadLocal.get()));
                System.out.println(String.format("threadlocal孙子线程值：%s", threadLocal.get()));
            }).start();
        }).start();
    }
}
